#coding= UTF-8

import wx

from Element import *

class Frame(wx.Frame):
    def __init__(self, game, title):
        super(Frame, self).__init__(None, title = title, size = (offset_x*2+stretch*game.N, 25+offset_y*2+stretch*game.M))
        self.game = game
        self.init()
        # self.CreateStatusBar()
        self.init_menu()
        self.init_body()
        self.Show(True)

    def init(self):
        self.click = False
        self.board = board(wx.Brush('gray'), self.game.N, self.game.M)
        self.atoms = []
        self.last = last(wx.Brush('red'))

    def init_body(self):
        # self.control = wx.TextCtrl(self, style = wx.TE_MULTILINE)
        # 绑定渲染窗口至Onpaint,这样每次resize窗口都会调用该函数
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
        self.Centre()

    def init_menu(self):

        # 设置菜单
        filemenu = wx.Menu()

        # wxWidgets标准ID
        menuAbout = filemenu.Append(wx.ID_ABOUT, u"关于", u"关于程序的信息")
        filemenu.AppendSeparator()
        menuExit = filemenu.Append(wx.ID_EXIT, u"退出", u"终止应用程序")

        # 创建菜单栏
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu, u"文件")
        self.SetMenuBar(menuBar)

        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)

    def OnAbout(self, event):
        # 创建一个带OK按钮的对话框,wx.OK是wxWidgets标准ID
        dlg = wx.MessageDialog(self, "here is text", "here is title", wx.OK)
        dlg.ShowModal()
        # 结束时关闭
        dlg.Destroy()

    def OnExit(self, event):
        self.Close(True)

    def OnClick(self, event):
        # print("how to speak chinese???")
        # for k in dir(event):
        #     print(k)
        # print(event.GetPosition())
        x, y = self.board.catch(event.x, event.y)
        # 能不能下,让game去判断
        self.game.go(x, y)

    def OnMouseEvents(self, event):
        if event.ButtonDown():
            self.click = True
        elif event.ButtonUp() and self.click:
            self.OnClick(event)
        else:
            self.click = False

    def draw(self):
        self.ClearBackground()
        dc = wx.ClientDC(self)
        # dc = wx.PaintDC(self)
        self.board.draw(dc)
        self.last.draw(dc)
        for atom in self.atoms:
            atom.draw(dc)

    def AddAtom(self, x, y, color):
        self.last.at(x, y)
        atm = atom(wx.Brush(color), x, y)
        self.atoms.append(atm)
        self.draw()

    def OnPaint(self, event):
        self.draw()
        # dc = wx.PaintDC(self)
        # self.board.draw(dc)
        # print("paint")
        # for atom in self.atoms:
        #     atom.draw(dc)
        # dc.DrawLine(50, 60, 190, 60)
        # dc.DrawLines(((20, 60), (100, 60), (100, 10),(20, 10), (20,60)))
        
    def OnEvent(self, event):
        pass

    def win(self, s):
        wx.MessageBox('%s is win'%s,'game_over',wx.OK)



def start(game):
    return Frame(game, 'editor')